package com.zhoujian.Proxy.DynamicProxy.AOP;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class CacheAspectj {
    @Pointcut("execution(* com.zhoujian.Proxy.DynamicProxy.AOP.DatabaseDataQuery.query(..))")
    public void pointcut(){}

    @Around("pointcut()")
    public String around(ProceedingJoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        String key = args[0].toString();
        //1.查询缓存，命中则返回
        String result = Cache.get(key);

        if (result != null){
            return result;
        }

        //未命中择取数据库查询，实际上是调用被代理bean的方法
        try {
            result = joinPoint.proceed().toString();

            //如果查询有结果，进行缓存
            Cache.put(key,result);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
